package com.lzh.dlykserver.controller;

import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.entity.TUser;
import com.lzh.dlykserver.query.PwdDTO;
import com.lzh.dlykserver.query.UserQuery;
import com.lzh.dlykserver.result.R;
import com.lzh.dlykserver.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 获取登录用户信息
     * @param authentication
     * @return
     */
    @GetMapping("/api/login/info")
    public R loginInfo(Authentication authentication) {
        // authentication 对象包含了登录用户的详细信息，这里使用强制类型转换获取用户对象
        TUser tUser = (TUser) authentication.getPrincipal();
        // 返回用户信息
        return R.OK(tUser);
    }

    /**
     * 免登录
     * @return
     */
    @GetMapping("/api/login/free")
    public R freeLogin() {
        return R.OK();
    }

    /**
     * 用户分页
     * @param current
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:list')")
    @GetMapping("/api/users")
    public R userPage(@RequestParam(value = "current", required = false) Integer current) {
        if (current == null) {
            current = 1;
        }
        PageInfo<TUser> pageInfo = userService.getUserByPage(current);
        return R.OK(pageInfo);
    }

    /**
     * 用户详情
     * @param id
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:view')")
    @GetMapping("/api/user/{id}")
    public R userDetail(@PathVariable("id") Integer id) {
        TUser tUser = userService.getUserById(id);
        return R.OK(tUser);
    }

    /**
     * 添加用户
     * @param usrQuery
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:add')")
    @PostMapping("/api/user")
    public R addUser(UserQuery usrQuery, @RequestHeader(value = "Authorization") String token) {
        usrQuery.setToken(token);
        int count = userService.saveUser(usrQuery);
        return count >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑用户
     * @param usrQuery
     * @param token
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:edit')")
    @PutMapping("/api/user")
    public R editUser(UserQuery usrQuery, @RequestHeader(value = "Authorization") String token) {
        usrQuery.setToken(token);
        int update = userService.updateUser(usrQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:delete')")
    @DeleteMapping("/api/user/{id}")
    public R deleteUser(@PathVariable("id") Integer id) {
        int del = userService.deleteUser(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:delete')")
    @DeleteMapping("/api/user")
    public R batchDeleteUser(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = userService.batchDeleteUser(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }

    /**
     * 获取所有拥有者
     * @return
     */
    @GetMapping("/api/owner")
    public R owner() {
        List<TUser> ownerList = userService.getOwnerList();
        return R.OK(ownerList);
    }

    /**
     * 修改密码
     * @param pwdDTO
     * @param token
     * @return
     */
    @PutMapping("/api/user/change-password")
    public R changePassword(@RequestBody PwdDTO pwdDTO,
                            @RequestHeader(value = "Authorization") String token) {
        pwdDTO.setToken(token);
        int count = userService.changePassword(pwdDTO);
        return count >= 1 ? R.OK() : R.FAIL();
    }
}
